//
//  HomeNavTitleView.swift
//  Stock_Stategy
//
//  Created by MayerF on 2017/9/6.
//  Copyright © 2017年 MayerF. All rights reserved.
//

import UIKit

class HomeNavTitleView: UIView, UITextFieldDelegate {
    let search_textFiled = UITextField()
//    let day_btn = UIButton()
    let searchImage = UIImageView(image: UIImage(named: "search"))
    
    //MARK: -
    //MARK: init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initCommon()
        initConstraints()
    }
    private func initCommon() {
        searchImage.frame = CGRect.init(origin: CGPoint(), size: CGSize(width: 30, height: 30))
        searchImage.contentMode = .center
        search_textFiled.leftView = searchImage
        search_textFiled.leftViewMode = .always
        search_textFiled.backgroundColor = UIColor(white: 1, alpha: 0.7)
        search_textFiled.layer.cornerRadius = 14
        search_textFiled.layer.masksToBounds = true
        search_textFiled.textColor = .white
        search_textFiled.font = UIFont.systemFont(ofSize: 12)
        search_textFiled.delegate = self
        search_textFiled.attributedPlaceholder = NSAttributedString.init(string: "请输入股票代码/首字母", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
        
//        day_btn.setImage(UIImage(named: "help"), for: .normal)
////        day_btn.setImage(UIImage(named: "sun"), for: .selected)
//        day_btn.addTarget(self, action: #selector(dayClick), for: .touchUpInside)
//        day_btn.isSelected = (ThemeManager.manager.themeType == 0 ? false: true)
    }
    private func initConstraints() {
        addSubview(search_textFiled)
//        addSubview(day_btn)
        
        search_textFiled.mas_makeConstraints { (make) in
            make!.top.equalTo()(self)?.offset()(0.0)
            make!.bottom.equalTo()(self)?.offset()(0.0)
            make?.left.equalTo()(self)?.offset()(10)
            make?.right.equalTo()(self)?.offset()(-10)
        }
//        day_btn.mas_makeConstraints { (make) in
//            make?.top.bottom().equalTo()(self.search_textFiled)
//            make?.right.equalTo()(self)?.offset()(0)
//            make?.width.equalTo()(40)
//        }
    }
    
    //MARK:-
    //MARK: UITextFieldDelegate
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kNotificationHome_SearchKey"), object: nil)
        return false
    }
    func dayClick() {
//        day_btn.isSelected = !day_btn.isSelected
//        ThemeManager.manager.themeType = (day_btn.isSelected ? 1: 0)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kNotificationHome_DayNightKey"), object: nil)
//        HZCommonTool.toolUpdateThemeType(String(ThemeManager.manager.themeType))
    }
    
    override var intrinsicContentSize: CGSize {
        return CGSize(width: self.frame.width, height: 28)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
